/**
 * Copyright (C) 2011 Andrew C. Love (DNC) <dnc.app.sup@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dnc.cloak.framework.messaging;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

public class MessageSender {
    private static Logger log = Logger.getLogger(MessageSender.class);
    
    private String destinationName = null;
    private Connection connection = null;
    private Session session = null;
    private MessageProducer messageProducer = null;
    private Queue m_queue = null;
     
    public MessageSender(String destinationNameArg){
        destinationName = destinationNameArg;
    }
    
    public void send(Message message) throws MessageException {
        log.info("Queue:" + destinationName);
        
        try {
			if (connection == null){
			    openConnection();        
			}
 
			ObjectMessage objectMessage = session.createObjectMessage();
			objectMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT);       
			objectMessage.setObject(message);
			    
			messageProducer.send(objectMessage);

			log.info("The " + objectMessage + " " + objectMessage.getJMSMessageID()  + " message sent to the " + m_queue.getQueueName() + " queue");
		} 
        catch (JMSException ex) {
        	throw new MessageException(ex);
		}             
    }
    
    public void openConnection() throws MessageException{
        try {
			if (connection != null){
			    closeConnection();
			}

			if (connection == null){
			    InitialContext ic = new InitialContext();
			    ConnectionFactory cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
			    //ConnectionFactory cf = (ConnectionFactory) ic.lookup("jca:/console.jms/ConnectionFactory/JCAManagedConnectionFactory/ConnectionFactory");
			    m_queue = (Queue) ic.lookup(destinationName);        
			    log.info("Queue:" + destinationName + " exists");
			    connection = cf.createConnection();
			    session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
			    messageProducer = session.createProducer(m_queue);
			}
		} 
        catch (NamingException ex) {
        	throw new MessageException(ex);
		} 
        catch (JMSException ex) {
        	throw new MessageException(ex);
		}        
    }
    
    public void closeConnection() throws MessageException{
        try {
			if (connection !=null){
			    //Close the JMS connection
			    connection.close();  
			    //Reset the member variables
			    connection = null;
			    session = null;
			    messageProducer = null;
			    m_queue = null;
			}
		} 
        catch (JMSException ex) {
        	throw new MessageException(ex);
		}        
    }
}

